<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    #mytxt {
      width: 400px;
      height: 200px;
    }
  </style>
</head>

<body>
  <textarea id="mytxt"></textarea>
  <span>还可以输入<span id="count">100</span>个字符</span>
  <script>
    var mytxt = document.getElementById("mytxt");
    var num = 0;
    var count = document.getElementById("count");
    mytxt.onkeyup = function () {
      // 100- 输入的字符串的长度
      var txtLen = mytxt.value.length + 1;//输入的长度
      console.log(txtLen)
      num = 100 - txtLen;
      if (num <= 0) {
        num = 0;
        mytxt.value = mytxt.value.substr(0, 100);
        count.style.color = "red"
      } else {
        count.style.color = "#000"
      }
      count.innerHTML = num;

    }
  </script>
</body>

</html>